# -*- coding: utf-8 -*-
# @Time    : 2020/10/19 22:14
# @Author  : liupengfei
# @File    : serializers.py
from rest_framework import serializers

from apps.system.serializers import UploadSerializer
from .models import VoteActivityApply, VoteActivity


class VoteActivityApplySerializer(serializers.ModelSerializer):
    class Meta:
        model = VoteActivityApply
        fields = '__all__'


class VoteActivityDetailSerializer(serializers.ModelSerializer):
    class Meta:
        model = VoteActivity
        fields = ('vote_way', 'vote_time', 'total_vote_count')


class VoteActivityApplyListSerializer(serializers.ModelSerializer):
    image_path = serializers.SerializerMethodField()

    def get_image_path(self, obj):
        if obj.images:
            return obj.images.first().get_url()
        return ''

    class Meta:
        model = VoteActivityApply
        fields = ('name', 'image_path', 'number', 'count')


class VoteActivityApplyDetailSerializer(serializers.ModelSerializer):
    face_image = serializers.SerializerMethodField()
    images = serializers.SerializerMethodField()
    vote_record = serializers.SerializerMethodField()

    def get_vote_record(self, obj):
        vote_activity = VoteActivity.objects.filter(activity=obj.member_activity.activity).first()
        member = self.context['request'].user.member
        return {
            'count': vote_activity.has_vote_count(member),
            'vote_way': vote_activity.vote_way,
            'vote_time': vote_activity.vote_time,
        }

    def get_face_image(self, obj):
        return obj.member_activity.member.face_url

    def get_images(self, obj):
        return UploadSerializer(obj.images, many=True).data

    class Meta:
        model = VoteActivityApply
        fields = ('name', 'number', 'images', 'introduce', 'count', 'face_image', 'vote_record')
